package com.paw.portal.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.validation.Valid;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.paw.common.domain.ProTableResponse;
import com.paw.common.domain.Result;
import com.paw.portal.entity.SysMenu;
import com.paw.portal.request.dto.SysMenuRequestDto;
import com.paw.portal.response.dto.SysMenuResponseDto;
import com.paw.portal.service.IAntdMenuService;
import com.paw.portal.service.ISysMenuService;
import com.paw.portal.util.PageUtil;
import com.paw.portal.util.QueryWrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author Rubble
 * @since 2021-08-21
 */
@Api(tags = "菜单权限控制器")
@RestController
public class SysMenuController {

  @Autowired
  private ISysMenuService sysMenuService;

  @Autowired
  private IAntdMenuService antdMenuService;

  @ApiOperation("菜单权限列表Tree")
  @PostMapping("/api/portal/sysMenu/list")
  public Result<List<SysMenuResponseDto>> getMenuTreeList(@Valid @RequestBody SysMenuRequestDto requestDto){
    return antdMenuService.getMenuTreeList(requestDto);
  }




  @ApiOperation("菜单权限列表")
  @PostMapping("/api/portal/sysMenu/pageList")
  public Result<List<SysMenuResponseDto>> getMenuPageList(@Valid @RequestBody SysMenuRequestDto requestDto){
    //    LambdaQueryWrapper<SysMenu> queryWrapper = QueryWrapperUtil.buildLambdaQueryWrapper(requestDto, SysMenu.class);
    return antdMenuService.getMenuPageList(requestDto);
  }

  @ApiOperation("新增菜单")
  @PostMapping("/api/portal/sysMenu/addMenu")
  public Result<SysMenuResponseDto> addMenu(@Valid @RequestBody SysMenuRequestDto requestDto){
    SysMenu menu = new SysMenu();
    BeanUtils.copyProperties(requestDto,menu);
    sysMenuService.save(menu);
    SysMenuResponseDto dto = new SysMenuResponseDto();
    BeanUtils.copyProperties(menu,dto);
    return Result.success(dto);
  }

  @ApiOperation("修改菜单")
  @PostMapping("/api/portal/sysMenu/updateMenu")
  public Result<SysMenuResponseDto> updateMenu(@Valid @RequestBody SysMenuRequestDto requestDto){
    Assert.notNull(requestDto.getId(),"id不能为空");
    SysMenu menu = new SysMenu();
    BeanUtils.copyProperties(requestDto,menu);
    sysMenuService.updateById(menu);
    SysMenuResponseDto dto = new SysMenuResponseDto();
    BeanUtils.copyProperties(menu,dto);
    return Result.success(dto);
  }


  @ApiOperation("删除菜单")
  @PostMapping("/api/portal/sysMenu/delMenu/{:id}")
  public Result<Boolean> delMenu(@PathVariable(value = "id") Long id){
    return antdMenuService.delMenuWithChildren(id);
  }



}
